package com.lmx.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.lmx.repoDomain.tmodel.Role;//huibati
import com.lmx.persistence.service.LoginService;
import com.lmx.repoDomain.tmodel.UserPrivate;

@Controller
@RequestMapping("/backStand")
public class LoginController {

	private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    LoginService loginServie;
    
    @Autowired
    HttpServletRequest request;
    
    //跳转登录页面
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String  login(ModelMap model) {
    	HttpSession session = request.getSession(); 
    	session.invalidate();
    	
    	model.put("flag", "");
		model.put("message", "");
		model.put("name", "");
        return "page/login";
    }  
    
    //跳转首页
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String  index(ModelMap model) {
        return "page/index";
    }    
    
    //后台管理登录
    @RequestMapping(value = "/backgroundLogin", method = RequestMethod.GET)
    public String  backgroundLogin(ModelMap model,String name , String pwd) {
    	UserPrivate user = new UserPrivate();
    	//验证loginName 
    	model.put("name", name);
    	if(name != null && !name.isEmpty()){
    		user.setLoginName(name);
    	}else{
    		model.put("flag", "0");
    		model.put("message", "登录名不能为空!");
            return "page/login";
    	}
    	UserPrivate resultUser = loginServie.getUserInfo(user);
    	String loginName = resultUser.getLoginName();
    	if(loginName != null && !loginName.isEmpty()){
    		Role role = resultUser.getRole();
    		if(role != null){
    			String roleCode = role.getCode();
    			if(roleCode.equals("admin")){
    			}else if(roleCode.equals("operator")){
    			}else if(roleCode.equals("system")){
    			}else{
    				model.put("flag", "0");
    	    		model.put("message", "该用户不存在!");
    	            return "page/login";
    			}
    		}
    	}else{
    		model.put("flag", "0");
    		model.put("message", "该用户不存在!");
            return "page/login";
    	}
    	//验证password
    	if(pwd != null && !pwd.isEmpty()){
    		String password = resultUser.getPassword();
    		if(pwd.equals(password)){
    		}else{
    			model.put("flag", "0");
        		model.put("message", "密码错误!");
                return "page/login";
    		}
    	}else{
    		model.put("flag", "0");
    		model.put("message", "密码不能为空!");
            return "page/login";
    	}
    	resultUser.setPassword(null);
    	HttpSession session = request.getSession(); 
    	session.setAttribute("user", resultUser); 
    	
    	session.setMaxInactiveInterval(120*60);//设置失效时长为2小时
    	model.put("flag", "1");
    	model.put("message", "登录成功!");
        return "page/login";
    }    
}
